#ifndef SERVICEPROVIDER_H
#define SERVICEPROVIDER_H

#include <QObject>
#include <QEventLoop>
#include <QJsonObject>
#include "ssloperation.h"



class ServiceProvider : public QObject
{
    Q_OBJECT
public:

    enum Method{
        NONE,
        CONNECT,
        GET,
        POST,
        CLOSE
    };

    enum ContentRequstType{
        PercentEncoding,
        JSONRequest
    };

    enum ContentAnswerType{
        HTML,
        JSONAnswer
    };

    struct ContentParameter{
        ContentParameter();
        ContentParameter(QString _key, QString _value, QString _hash_key);
        QString     key;
        QString     value;
        QString     hash_key;
        ContentParameter& operator=(const ContentParameter &other);
        bool operator==(const ContentParameter &other);
    };

    struct OperationContent{
        OperationContent();
        OperationContent(Method _method, QString _address,
                          int _percent_completed=0,
                          QList<ContentParameter> _ReceiveParamenters=QList<ContentParameter>(),
                          QList<ContentParameter> _SendParamenters=QList<ContentParameter>(),
                          QStringList _errorStrings=QStringList(),
                          QString _errorMessage="Wrong login/password!",
                          ContentAnswerType _answerType=HTML,
                          ContentRequstType _requestType=PercentEncoding,
                          void (*_func)()=NULL
                          );
        QString                 address;
        Method                  method;
        QStringList             errorStrings;
        QString                 errorMsg;
        QList<ContentParameter> ReceiveParamenters;
        ContentAnswerType       answerType;
        QList<ContentParameter> SendParamenters;
        ContentRequstType       requestType;
        int                     percent_completed;
        void                    (*func)();
        OperationContent& operator=(const OperationContent &other);
        bool operator==(const OperationContent &other);
    };


    enum Operation{
        NOOperation,
        KyivstarBalance,
        TriolanBalance,
        KyivEnergoBalance,
        PrivatKyivstarPayment
    };
//=======================================================================================================
    explicit        ServiceProvider(QObject *parent = 0);
    explicit        ServiceProvider(QList<OperationContent> *operList, QObject *parent = 0);
    ~ServiceProvider();

    void            Engine();
    void            DoCONNECT();
    void            DoGET();
    void            DoPOST();
    void            DoCLOSE();

    void            SetOperList(QList<OperationContent> *operList);
    int             getUserParameter();

public slots:
    void            Start();
    virtual void    CreateList()=0;
    void            Error(QString error);
    void            Errors(QList<QSslError> err_list);
    virtual void    OperProcessing();
    virtual QString Message4SmsCodeWindow();

private slots:

    void            SslSocketError(QList<QSslError> err_list);
    void            socketError(QAbstractSocket::SocketError);
    void            socketReadyRead();
    void            sslErrorsSlot(QNetworkReply* reply,QList<QSslError> errList);
    void            Encrypted();
    void            finishedGetRequestSlot(QNetworkReply* reply);
    void            finishedGetRequestSlot2(QNetworkReply* reply);
    void            finishedPostRequestSlot(QNetworkReply*reply);
    void            finishedOper();
    void            OperationIncrement();
virtual    void     SetPassword(QString passw);
virtual    void     SetMobileNumber(QString phone);

signals:
    void            JobFinished();
    void            OperationFinished(QString );
    void            OperationFinished(QJsonObject );
    void            OperFinished();
    void            OperationError(QString );
    void            OperationErrors(QList<QSslError> );
    void            PercentFinished(int);
    void            CurrentOperaionIncrement();
    void            NeedPassword(QString);
    void            NeedMobileNumber();

protected:
    int             m_userParameter;
    QSslSocket      *this_socket;
    QNetworkAccessManager *m_manager;
    QList<OperationContent> *m_operList;
    QHash<QString, QString>  m_hash;
    QJsonObject    m_resultJson;
    int            m_currentOperation;
    bool           m_finishedOperation;

private:
    QEventLoop     m_loop;
    QSsl::SslProtocol       m_protocol;

    void           ParseAnswer(QByteArray &bytes);
    QString        TakeAddress(QString adress_string);

signals:

public slots:
};

#endif // SERVICEPROVIDER_H
